package com.cyl.wms.service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.wms.domain.AssetCategory;
import com.github.pagehelper.PageHelper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.cyl.wms.mapper.AssetItemMapper;
import com.cyl.wms.domain.AssetItem;
import com.cyl.wms.pojo.query.AssetItemQuery;

/**
 * 资产清单表Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class AssetItemService {
    @Autowired
    private AssetItemMapper assetItemMapper;

    @Autowired
    private AssetCategoryService assetCategoryService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询资产清单表
     *
     * @param id 资产清单表主键
     * @return 资产清单表
     */
    public AssetItem selectById(Integer id) {
        AssetItem res = assetItemMapper.selectById(id);
        AssetCategory cate = assetCategoryService.selectById(res.getCategoryId());
        if (cate != null) {
            res.setCategoryName(cate.getName());
        }
        if (res.getAdminId() != null && res.getAdminId() != 0) {
            res.setAdminName(userService.getUserNameById(res.getAdminId()));
        }
        return res;
    }

    /**
     * 查询资产清单表列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 资产清单表
     */
    public List<AssetItem> selectList(AssetItemQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<AssetItem> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        String number = query.getNumber();
        if (!StringUtils.isEmpty(number)) {
            qw.eq("number", number);
        }
        String nameLike = query.getNameLike();
        if (!StringUtils.isEmpty(nameLike)) {
            qw.like("name", nameLike);
        }
        Integer categoryId = query.getCategoryId();
        if (categoryId != null) {
            qw.eq("category_id", categoryId);
        }
        String rfid = query.getRfid();
        if (!StringUtils.isEmpty(rfid)) {
            qw.eq("rfid", rfid);
        }
        String brand = query.getBrand();
        if (!StringUtils.isEmpty(brand)) {
            qw.eq("brand", brand);
        }
        String model = query.getModel();
        if (!StringUtils.isEmpty(model)) {
            qw.eq("model", model);
        }
        String buyWay = query.getBuyWay();
        if (!StringUtils.isEmpty(buyWay)) {
            qw.eq("buy_way", buyWay);
        }
        LocalDateTime buyTime = query.getBuyTime();
        if (buyTime != null) {
            qw.eq("buy_time", buyTime);
        }
        BigDecimal buyPrice = query.getBuyPrice();
        if (buyPrice != null) {
            qw.eq("buy_price", buyPrice);
        }
        Integer adminId = query.getAdminId();
        if (adminId != null) {
            qw.eq("admin_id", adminId);
        }
        List<AssetItem> list = assetItemMapper.selectList(qw);
        for(AssetItem item : list) {
            AssetCategory cate = assetCategoryService.selectById(item.getCategoryId());
            if (cate != null) {
                item.setCategoryName(cate.getName());
            }

            if (item.getAdminId()!= null && item.getAdminId()> 0) {
                item.setAdminName(userService.getUserNameById(item.getAdminId()));
            }

        }
        return list;
    }

    /**
     * 新增资产清单表
     *
     * @param assetItem 资产清单表
     * @return 结果
     */
    public int insert(AssetItem assetItem) {
        assetItem.setDelFlag(0);
        return assetItemMapper.insert(assetItem);
    }

    /**
     * 修改资产清单表
     *
     * @param assetItem 资产清单表
     * @return 结果
     */
    public int update(AssetItem assetItem) {
        return assetItemMapper.updateById(assetItem);
    }

    /**
     * 批量删除资产清单表
     *
     * @param ids 需要删除的资产清单表主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        return assetItemMapper.updateDelFlagByIds(ids);
    }

    /**
     * 删除资产清单表信息
     *
     * @param id 资产清单表主键
     * @return 结果
     */
    public int deleteById(Long id) {
        Long[] ids = {id};
        return assetItemMapper.updateDelFlagByIds(ids);
    }
}
